/*
 * This file is part of aion-unique <aion-unique.org>.
 *
 *  aion-unique is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  aion-unique is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with aion-unique.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionemu.gameserver.network.aion.clientpackets;

import org.apache.log4j.Logger;

import com.aionemu.gameserver.model.gameobjects.player.Player;
import com.aionemu.gameserver.network.aion.AbstractAionClientPacket;
import com.aionemu.gameserver.services.MailService;

/**
 * @author Lyahim, kosyachok
 *
 */
public class CM_SEND_MAIL extends AbstractAionClientPacket
{

	private static final Logger	log	= Logger.getLogger(CM_SEND_MAIL.class);

	private String	recipientName;
	private String	title;
	private String	message;
	private int		itemObjId;
	private long	itemCount;
	private long	kinahCount;
	private int		express;

	public CM_SEND_MAIL(int opcode)
	{
		super(opcode);
	}

	@Override
	protected void readImpl()
	{
		recipientName = readS();
		title = readS();
		message = readS();
		itemObjId = readD();
		itemCount = readQ();
		kinahCount = readQ();
		express = readC();
	}

	@Override
	protected void runImpl()
	{
		Player player = getClient().getActivePlayer();
		if (kinahCount < -1 || itemCount < -1)
		{
			log.warn("[AUDIT] Possible client hack Player: "+player.getName()+" Account name: "+player.getAcountName()+toString());
			getClient().closeNow();
			return;
		}

		if (kinahCount < 0)
			kinahCount = 0;
		if (itemCount < 0)
			itemCount = 0;
		if (!player.isTrading())
		{

			if (express == 0)
				MailService.getInstance().sendMail(player, recipientName, title, message, itemObjId, itemCount, kinahCount, false);
			if (express == 1)
				MailService.getInstance().sendMail(player, recipientName, title, message, itemObjId, itemCount, kinahCount, true);
		}
	}

	@Override
	public String toString() {
		return "CM_SEND_MAIL [recipientName=" + recipientName + ", title="
				+ title + ", message=" + message + ", itemObjId=" + itemObjId
				+ ", itemCount=" + itemCount + ", kinahCount=" + kinahCount
				+ ", express=" + express + "]";
	}
	
}
